package gestionedt.models;

import java.io.Serializable;
import java.util.Collection;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;

/**
 *
 *
 */
@Entity
@Table(name = "creneau")
@NamedQueries({@NamedQuery(name = "Creneau.findAll",
                           query = "SELECT c FROM Creneau c"),
               @NamedQuery(name = "Creneau.findByNumero",
                           query = "SELECT c FROM Creneau c WHERE c.numero = :numero")})
public class Creneau implements Serializable {

    /**
     * Nombre de jours géré par l'emploi du temps.
     */
    public final static int NB_JOURS = 5;

    public static int getNbJours() {
        return NB_JOURS;
    }

    public static String getJour(int numJour) {
        switch(numJour) {
            case 1: return "lundi";
            case 2: return "mardi";
            case 3: return "merdredi";
            case 4: return "jeudi";
            case 5: return "vendredi";
            case 6: return "samedi";
            case 7: default: return "dimanche";
        }
    }

    /**
     * Nombre de créneaux par jour géré par l'emploi du temps.
     */
    public final static int NB_CRENEAUX_PAR_JOUR = 4;

    public static int getNbCreneauxParJour() {
        return NB_CRENEAUX_PAR_JOUR;
    }

    public static String getPlageHoraire(int numHeure) {
        switch(numHeure) {
            case 1: return "8h-10h";
            case 2: return "10h-12h";
            case 3: return "14h-16h";
            case 4: return "16h-18h";
            case 5: default: return "18h-20h";
        }
    }

    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @Column(name = "numero")
    private Integer numero;
    @JoinTable(name = "indisponibilite_groupe", joinColumns = {@JoinColumn(name = "numeroCreneau", referencedColumnName = "numero")}, inverseJoinColumns = {@JoinColumn(name = "idGroupe", referencedColumnName = "id")})
    @ManyToMany
    private Collection<Groupe> groupeCollection;
    @JoinTable(name = "indisponibilite_enseignant", joinColumns = {@JoinColumn(name = "numeroCreneau", referencedColumnName = "numero")}, inverseJoinColumns = {@JoinColumn(name = "idEnseignant", referencedColumnName = "id")})
    @ManyToMany
    private Collection<Enseignant> enseignantCollection;
    @OneToMany(mappedBy = "numCreneau")
    private Collection<Prestation> prestationCollection;

    public Creneau() {
    }

    public Creneau(Integer numero) {
        this.numero = numero;
    }

    public Integer getNumero() {
        return numero;
    }

    public int getNumJour() {
        int numC = getNumero().intValue();
        return ((numC - 1) / 4) + 1;
    }

    public String getJour() {
        return Creneau.getJour(getNumJour());
    }

    public int getNumHeure() {
        int numC = getNumero().intValue();
        return ((numC - 1) % 4) + 1;
    }

    public String getPlageHoraire() {
        return Creneau.getPlageHoraire(getNumHeure());
    }

    public void setNumero(Integer numero) {
        this.numero = numero;
    }

    public Collection<Groupe> getGroupesIndispo() {
        return groupeCollection;
    }

    public void setGroupesIndispo(Collection<Groupe> groupeCollection) {
        this.groupeCollection = groupeCollection;
    }

    public Collection<Enseignant> getEnseignantsIndispo() {
        return enseignantCollection;
    }

    public void setEnseignantsIndispo(Collection<Enseignant> enseignantCollection) {
        this.enseignantCollection = enseignantCollection;
    }

    public Collection<Prestation> getPrestations() {
        return prestationCollection;
    }

    public void setPrestations(Collection<Prestation> prestationCollection) {
        this.prestationCollection = prestationCollection;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (numero != null ? numero.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Creneau)) {
            return false;
        }
        Creneau other = (Creneau) object;
        if ((this.numero == null && other.numero != null) || (this.numero != null && !this.numero.equals(other.numero))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        // on suppose ici qu'une semaine est découpée en 20 créneaux
        int numC = getNumero().intValue();
        if(numC > 20) // au cas où le numéro de créneau dépasserait 20 :
            return "créneau n°" + numC;
        String creneau = "";
        switch(getNumJour()) {
            case 1 : creneau += "lundi";     break;
            case 2 : creneau += "mardi";     break;
            case 3 : creneau += "mercredi";  break;
            case 4 : creneau += "jeudi";     break;
            case 5 : creneau += "vendredi";  break;
        }
        switch(getNumHeure()) {
            case 1 : creneau += " de 8h à 10h";  break;
            case 2 : creneau += " de 10h à 12h"; break;
            case 3 : creneau += " de 14h à 16h"; break;
            case 4 : creneau += " de 16h à 18h"; break;
        }
        return creneau;
    }

}
